package com.chinanet.netelementmodel;

import com.chinanet.entry.netelement.Attr;
import com.chinanet.entry.netelement.AttrType;
import com.chinanet.entry.netelement.WorkInterFace;
import com.chinanet.utils.webservice.AttrsIn;
import com.chinanet.utils.webservice.WebService;

import java.util.HashMap;
import java.util.Map;

/**
 * hp宽带帐号连接session查询
 * @author xuzhi
 *
 */

public class HP_WebService_Imp_findBySession implements WorkInterFace {

	@Override
	public String getName() {
		return "hp宽带帐号连接session查询";
	}

	@Override
	public String getDesciption() {
		return "";
	}

	@Override
	public int getVersion() {
		return 0;
	}

	@Override
	public Attr[] getInAttrs() {
		Attr[] in=new Attr[1];
		in[0]=new Attr("Login", "值表示登录名全称：如hzhza12345678;对于普通宽带用户,不需要带后缀@zj(默认为@zj),但是对于卡用户,必须带后缀@tyt", AttrType.TYPE_String, true);
		return in;
	}

	@Override
	public Attr[] getOutAttrs() {
		Attr[] out = new Attr[12];
		out[0]=new Attr("ReturnCode", "0：成功；其它：失败", AttrType.TYPE_Int, true);
		out[1]=new Attr("Result", "返回结果的根节点", AttrType.TYPE_String, true);
		out[2]=new Attr("error", "值表示错误信息, 比如请求报文数据格式不对", AttrType.TYPE_String, true);
		out[3]=new Attr("sessions", "表示一组session,如果有会话信息, 那么CDRs节点将包含session子节点", AttrType.TYPE_String, true);
		out[4]=new Attr("session", "表示一条会话信息", AttrType.TYPE_String, true);
		out[5]=new Attr("login", "值表示完整的用户登录名", AttrType.TYPE_String, true);
		out[6]=new Attr("start_t", "值表示会话开始时间", AttrType.TYPE_String, true);
		out[7]=new Attr("user_ip", "值表示用户IP", AttrType.TYPE_String, true);
		out[8]=new Attr("access_server_ip", "值表示接入服务器IP", AttrType.TYPE_String, true);	
		out[9]=new Attr("log", "服务器返回的soapxml", AttrType.TYPE_String, true);
		out[10]=new Attr("Resultflag", "判断查询结果是否为空的标记，0：非空；1：空", AttrType.TYPE_Int, true);
		out[11]=new Attr("Errormsg", "服务器返回的错误信息,当ReturnCode为0的时候就不必去读取", AttrType.TYPE_String, false);
		return out;
	}

	@Override
	public Map<String, Object> init(Map<String, Object> data) {
		AttrsIn attrsIn=new AttrsIn();
		WebService webService=new WebService();
		Map<String,Object> map=new HashMap<String, Object>();
		//Get into arguments
		attrsIn.setClientSysName("CDMA");
		attrsIn.setFunctionName("gethpsession");
		attrsIn.setRequestMessage("<DN>"+(String) data.get("Login")+"</DN>");
		//Get soapxml
		String returnXml=webService.WebServiceInit(attrsIn);
		String flag=webService.getPattern("flag", returnXml);
		if(flag.equals("true")){
			map.put("ReturnCode", 0);
			map.put("Result", webService.getPattern("Result", returnXml));
			map.put("error", webService.getPattern("error", returnXml));
			if(webService.getPattern("Result", returnXml).equals("<sessions/>")){
				map.put("Resultflag", 1);
			}else {
				map.put("Resultflag", 0);
			}
			map.put("sessions", webService.getPattern("sessions", returnXml));
			map.put("session", webService.getPattern("session", returnXml));
			map.put("login", webService.getPattern("login", returnXml));
			map.put("start_t", webService.getPattern("start_t", returnXml));
			map.put("user_ip", webService.getPattern("user_ip", returnXml));
			map.put("access_server_ip", webService.getPattern("access_server_ip", returnXml));
			map.put("log", webService.dealkouhao(returnXml));
			map.put("Errormsg", "");
			map.put("flag",1);
		}else{
			map.put("flag",0);
			map.put("ReturnCode", 1);
			map.put("Result", "");
			map.put("error", "");
			map.put("sessions","" );
			map.put("session", "");
			map.put("login","");
			map.put("start_t","" );
			map.put("user_ip", "");
			map.put("access_server_ip","");
			map.put("log", webService.dealkouhao(returnXml));
			map.put("Errormsg", "网元侧执行出错，处理请求失败");
		}
		
		return map;
	}

}
